;*****************************************************************************************************
;+
; NAME:
;       IGL_TEXTTOOL
;
; PURPOSE:
;
;       This is a CATTOOL object representing the addition/deletion of
;       an IGL_Text object in a IGL_Window. It is written to be able
;       to undo/redo text annotation operations in a window.
;
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
; SUPERCLASSES:
;
;       CATTOOL (Catalyst Library)
;
; SYNTAX:
;
;       tool = Obj_New('IGL_TextTool')
;
;
; CLASS_STRUCTURE:
;
;       class = {IGL_TextTool, $      
;                INHERITS CATTOOL $   ; Inherits the CATTOOL object class.
;               }
;
;
; MODIFICATION_HISTORY:
;
;       Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
;+
; NAME:
;       IGL_TextTool::APPLY
;
; PURPOSE:
;
;       This method apply the tool by adding the target IGL_Text
;       object to the window. The object reference of the window is
;       stored in the cache.
;
; SYNTAX:
;
;       self -> Apply
;
; ARGUMENTS:
;
;       None.
;
; KEYWORDS:
;
;       DRAW:   Set this keyword to call the draw method of the target
;               object after the tool is applied.
;
;-
;*****************************************************************************************************
PRO IGL_TextTool::Apply, Draw=draw

   @cat_pro_error_handler

   ;; Make sure there is a valid IGL_TEXT target object.
   IF Obj_ISA_Valid(self._theTarget,'IGL_TEXT') EQ 0 THEN RETURN

   ;; Make sure the cache data is a valid IGL_Window object reference
   IF PTR_VALID(self._theCache) EQ 0 THEN RETURN
   window = *self._theCache
   IF OBJ_ISA_VALID(window,'IGL_WINDOW') EQ 0 THEN RETURN
   
   ;; Add the IGL_TEXT object to the window
   window -> AddGraphic, self._theTarget

   ;; Update the window property panel
   window -> GetProperty, PropertyPanel=PropertyPanel
   IF OBJ_VALID(PropertyPanel) THEN PropertyPanel -> UpdateTreeBrowser
   
   ; Need a draw method?
   IF Keyword_Set(draw) THEN self._theTarget -> Draw

   ;; Report completion
   self -> Report, /Completed
   
END

;*****************************************************************************************************
;+
; NAME:
;       IGL_TextTool::GETPROPERTY
;
; PURPOSE:
;
;       This method is used to get the object's properties.
;
; SYNTAX:
;
;       self -> GetProperty
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     _REF_EXTRA:     Any keywords appropriate for the superclass GetProperty method.
;-
;*****************************************************************************************************
PRO IGL_TextTool::GetProperty, _REF_EXTRA=extraKeywords

   @cat_pro_error_handler

   IF (N_ELEMENTS (extraKeywords) GT 0) THEN self -> CATTOOL::GetProperty, _EXTRA=extraKeywords

   self -> Report, /Completed

END

;*****************************************************************************************************
;+
; NAME:
;       IGL_TextTool::REDO
;
; PURPOSE:
;
;       This method redo the tool on the target object.
;
; SYNTAX:
;
;       self -> Redo
;
; ARGUMENTS:
;
;       None.
;
; KEYWORDS:
;
;       DRAW:    Set this keyword to call the draw method of the
;                target object after the tool is applied.
;
;-
;*****************************************************************************************************
PRO IGL_TextTool::Redo, Draw=draw

   @cat_pro_error_handler
   
   self -> Apply, Draw=draw

   self -> Report, /Completed

END


;*****************************************************************************************************
;+
; NAME:
;       IGL_TextTool::SETPROPERTY
;
; PURPOSE:
;
;       This method is used to set the object's properties.
;
; SYNTAX:
;
;       self -> SetProperty
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     _EXTRA:       Any keyword accepted by the SetProperty method of
;                   the CATTOOL superclass.
;-
;*****************************************************************************************************
PRO IGL_TextTool::SetProperty, TARGET=target, $                              
                               _EXTRA=extraKeywords

   @cat_pro_error_handler

   ;; Make sure the target is a valid IGL_TEXT object
   IF N_Elements(target) NE 0 THEN BEGIN
      
      IF Obj_IsA_Valid(target, 'IGL_TEXT') THEN self._theTarget = target $
      ELSE message, 'The target object is invalid or not a sub-class of IGL_TEXT.'
      
   ENDIF
   
   IF (N_ELEMENTS (extraKeywords) GT 0) THEN self -> CATTOOL::SetProperty, _EXTRA=extraKeywords

   ;; Report completion
   self -> Report, /Completed

END

;*****************************************************************************************************
;+
; NAME:
;       IGL_TextTool::UNDO
;
; PURPOSE:
;
;       This method undo the tool by removing the target IGL_Text
;       object from the window. The object reference of the window is
;       stored in the cache.
;
; SYNTAX:
;
;       self -> Undo
;
; ARGUMENTS:
;
;       None.
;
; KEYWORDS:
;
;       DRAW:    Set this keyword to call the draw method of the
;                target object after the tool is applied.
;
;-
;*****************************************************************************************************
PRO IGL_TextTool::Undo, DRAW=draw

   @cat_pro_error_handler

   ;; Make sure we have a valid IGL_TEXT object
   IF (obj_isa_valid(self._theTarget,'IGL_TEXT') EQ 0) THEN return
   
   ; We need a valid IGL_Window reference in the cache
   IF Ptr_Valid(self._theCache) EQ 0 THEN RETURN   
   window = *self._theCache
   IF (obj_isa_valid(window,'IGL_WINDOW') EQ 0) THEN RETURN

   ;; Delete the target's control panel
   self._theTarget -> GetProperty, ControlPanel=cp
   IF OBJ_VALID(cp) THEN obj_destroy, cp

   ;; Undo the text annotation by removing it from window
   self._theTarget -> SetProperty, AUTO_DESTROY=0
   window -> RemoveGraphic, self._theTarget
   self._theTarget -> SetProperty, AUTO_DESTROY=1

   ;; Update the window property panel
   window -> GetProperty, PropertyPanel=PropertyPanel
   IF OBJ_VALID(PropertyPanel) THEN PropertyPanel -> UpdateTreeBrowser

   ;; Need a draw?
   IF Keyword_Set(draw) THEN self._theTarget -> Draw

   ;; Report completion
   self -> Report, /Completed

END

;*****************************************************************************************************
;+
; NAME:
;       IGL_TextTool::CLEANUP
;
; PURPOSE:
;
;       This is the IGL_TextTool object class destructor method.
;
; SYNTAX:
;
;       Called automatically when the object is destroyed.
;
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     None.
;-
;*****************************************************************************************************
PRO IGL_TextTool::CLEANUP

   @cat_pro_error_handler

   self -> CATTOOL::CLEANUP

   self -> Report, /Completed
END


;*****************************************************************************************************
;+
; NAME:
;       IGL_TextTool::INIT
;
; PURPOSE:
;
;       This is the IGL_TextTool object class creator method.
;
; SYNTAX:
;
;       Called automatically when the object is created.
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     _EXTRA:    Any keywords accepted by the CATTOOL::Init method.
;
;-
;*****************************************************************************************************
FUNCTION IGL_TextTool::INIT, TARGET=target, $
                             _Extra=extraKeywords


   @cat_func_error_handler

   ;; The target must be a IGL_TEXT object
   IF N_Elements(target) NE 0 THEN $
   BEGIN
     IF Obj_IsA_Valid(target, 'IGL_TEXT') EQ 0 THEN $
        Message, 'The target object is invalid or not a sub-class of IGL_TEXT.'
   ENDIF

   ;; Call superclass INIT method
   ok = self -> CATTOOL::Init(target=target,_Extra=extraKeywords)

   IF (NOT ok) THEN RETURN, 0

   RETURN, 1

END


;*****************************************************************************************************
;
; NAME:
;       IGL_TEXTTOOL CLASS DEFINITION
;
; PURPOSE:
;
;       This is the IGL_TextTool object's structure definition code.
;
;*****************************************************************************************************
PRO IGL_TextTool__DEFINE, class

  COMPILE_OPT idl2, HIDDEN

  class = {IGL_TextTool, $      
           INHERITS CATTOOL $   ; Inherits the CATTOOL object class.
          }

  struct_hide, class

END
